package ve.com.wayoyo.wuml_case.DibujoDiagramas;

import ve.com.wayoyo.wuml_case.R;
import ve.com.wayoyo.wuml_case.UserPref;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

public class AtributoComponente extends Dialog {

	EditText etName;
	private boolean atr_final;
	private boolean atr_static;
	private boolean atr_volatile;
	private String modificador;
	private String nombreVariable;
	private String tipoDato;

	public AtributoComponente(Context context) {
		super(context);
	}
	
	public void alert(String mensaje)
	{
		Toast.makeText(this.getContext(), mensaje , Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.atributo_componente_layout);
		int pageWidth = UserPref.getInstancia().getWinWidth();
		int pageHeight = UserPref.getInstancia().getWinHeight();
		LayoutParams params = getWindow().getAttributes();
		if (pageWidth<410){
			params.width = pageWidth;
			params.height = pageHeight;
		}else{
			params.width = 410;
			params.height = 310;   
		}		     
		getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
		setTitle("Nuevo Atributo");
		atr_final = false;
		atr_static = false;
		atr_volatile = false;
		this.setModificadores();		
		this.setok();
	}
	
	public void setModificadores()
	{
		final CheckBox cb_final = (CheckBox)findViewById(R.id.modif_final);
		final CheckBox cb_volatile = (CheckBox)findViewById(R.id.modif_volatile);
		CheckBox cb_static = (CheckBox)findViewById(R.id.modif_static);
		cb_final.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
	        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		        if (isChecked) {
		        	atr_volatile = false;
		        	cb_volatile.setChecked(false);
		        	atr_final = true;
		        }
		        else {
		        	atr_final = false;
		        }
	        }
        });
		
		cb_static.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
	        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		        if (isChecked) {
		        	atr_static = true;
		        }
		        else {
		        	atr_static = false;
		        }
	        }
        });
		
		cb_volatile.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
	        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		        if (isChecked) {
		        	atr_final = false;
		        	cb_final.setChecked(false);
		        	atr_volatile = true;
		        }
		        else {
		        	atr_volatile = false;
		        }
	        }
        });

		RadioGroup rg_alcance = (RadioGroup)findViewById(R.id.alcane_atributo);
		modificador = "default";
		rg_alcance.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
		        public void onCheckedChanged(RadioGroup group, int checkedId) {
		        	if (checkedId == R.id.radio_button_public)
		        		modificador = "public";
		        	else if (checkedId == R.id.radio_button_private)
		        		modificador = "private";
		        	else if (checkedId == R.id.radio_button_protected)
		        		modificador = "protected";
		        	else
		        		modificador = "default";
		        }
	        });
	}

	public void setok(){
		Button buttonOK = (Button) findViewById(R.id.btAtributo);
		buttonOK.setText("Agregar");
		buttonOK.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				nombreVariable = ((EditText) findViewById(R.id.nombreAtributoNuevo)).getText().toString();
				tipoDato = ((EditText) findViewById(R.id.nuevoTipoDato)).getText().toString();
				VistaDiagrama vista = VistaDiagrama.getVista();
				CompAtributo comp = new CompAtributo(nombreVariable,modificador.toLowerCase(),tipoDato);
				comp.setFinal(atr_final);
				comp.setStatic(atr_static);
				comp.setVolatile(atr_volatile);
				Diagrama diag = vista.getDiagrama();
				((DiagramaClases) diag).agregarAtributo(comp);				
				vista.invalidate();
				AtributoComponente.this.dismiss();
			}
		});

	}
}